#include "filesystem.h"
#include <time.h>
#include <fstream>
#include <sstream>
#include <iostream>

FileSystem::FileSystem()
{
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
FileSystem::~FileSystem()
{
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void FileSystem::writeFile( std::string _fname )
{
 const char *newfname = _fname.c_str();
 ofstream myfile(newfname);
 string txt;
 string line;
 string file_title;

// print title and time 
 file_title += "File generated by EqC  -  ";
 time_t rawtime;
 time (&rawtime);
 file_title += ctime(&rawtime);
 myfile << file_title << "\n";

 vector<Data> newDataSheet = CONTROLLER.getDataSheet();
 std::vector<Data>::iterator it;

 bool check = CONTROLLER.calcMagSetted();
 int sizeyear = 0; int sizedate = 0;  int sizehour = 0;    int sizelat = 0;
 int sizelng = 0;  int sizeerror = 0; int sizestate = 0;   int sizedepth = 0;
 int sizemag = 0;  int sizetype = 0;  int sizecat = 0;     int sizeIo = 0;
 int sizearea = 0; int sizelocal = 0; int sizecomment = 0; int sizeMw1 = 0;
 int sizeMw2 = 0;  int sizeMw3 = 0;   int sizeMw = 0;

 for(it = newDataSheet.begin(); it != newDataSheet.end(); ++it)
 {
  stringstream year;  stringstream lat;   stringstream lng;
  stringstream error; stringstream depth; stringstream mag; 
  stringstream type;  stringstream area;  stringstream Mw1;
  stringstream Mw2;   stringstream Mw3;   stringstream Mw;

  year << it->getyear(); txt = year.str();
  if (txt.size()>sizeyear)
  {
   sizeyear = txt.size();
  }
  txt = it->getdate().c_str();
  if (txt.size()>sizedate)
  {
   sizedate = txt.size();
  }
  txt = it->gethour().c_str();
  if (txt.size()>sizehour)
  {
   sizehour = txt.size();
  }
  lat << it->getlat(); txt = lat.str();
  if (txt.size()>sizelat)
  {
   sizelat = txt.size();
  }
  lng << it->getlong(); txt = lng.str();
  if (txt.size()>sizelng)
  {
   sizelng = txt.size();
  }
  error << it->geterror(); txt = error.str();
  if (txt.size()>sizeerror)
  {
   sizeerror = txt.size();
  }
  txt = it->getstate().c_str();
  if (txt.size()>sizestate)
  {
   sizestate = txt.size();
  }
  depth << it->getdepth(); txt = depth.str();
  if (txt.size()>sizedepth)
  {
   sizedepth = txt.size();
  }
  mag << it->getmag(); txt = mag.str();
  if (txt.size()>sizemag)
  {
   sizemag = txt.size();
  }
  type << it->gettype(); txt = type.str();
  if (txt.size()>sizetype)
  {
   sizetype = txt.size();
  }
  txt = it->getcat();
  if (txt.size()>sizecat)
  {
   sizecat = txt.size();
  }
  txt = it->getIo().c_str();
  if (txt.size()>sizeIo)
  {
   sizeIo = txt.size();
  }
  area << it->getarea(); txt = area.str();
  if (txt.size()>sizearea)
  {
   sizearea = txt.size();
  }
  txt = it->getlocal().c_str();
  if (txt.size()>sizelocal)
  {
   sizelocal = txt.size();
  }
  txt = it->getcomment().c_str();
  if (txt.size()>sizecomment)
  {
   sizecomment = txt.size();
  }


  if (check)
  {

   Mw1 << it->getMw1(); txt = Mw1.str();
   if (txt.size()>sizeMw1)
   {
    sizeMw1 = txt.size();
   }

   Mw2 << it->getMw2(); txt = Mw2.str();
   if (txt.size()>sizeMw2)
   {
    sizeMw2 = txt.size();
   }

   Mw3 << it->getMw3(); txt = Mw3.str();
   if (txt.size()>sizeMw3)
   {
    sizeMw3 = txt.size();
   }

   Mw << it->getMw(); txt = Mw.str();
   if (txt.size()>sizeMw)
   {
    sizeMw = txt.size();
   }

  }
 }
 int pos = 0;
 int maxSize = sizeyear + sizedate  + sizehour    + sizelat   + 
               sizelng  + sizeerror + sizestate   + sizedepth +
               sizemag  + sizetype  + sizecat     + sizeIo    +
               sizearea + sizelocal + sizecomment + sizeMw1   + 
               sizeMw2  + sizeMw3   + sizeMw      + 60;
 for(it = newDataSheet.begin(); it != newDataSheet.end(); ++it)
 {
  stringstream year;  stringstream lat;   stringstream lng;
  stringstream error; stringstream depth; stringstream mag; 
  stringstream type;  stringstream area;  stringstream Mw1;
  stringstream Mw2;   stringstream Mw3;   stringstream Mw;

  line.resize(maxSize);

  year << it->getyear(); txt = year.str();    line.insert(pos,txt); pos = pos + sizeyear + 4;
  txt = it->getdate().c_str();                line.insert(pos,txt); pos = pos + sizedate + 4;
  txt = it->gethour().c_str();                line.insert(pos,txt); pos = pos + sizehour + 4;
  lat << it->getlat(); txt = lat.str();       line.insert(pos,txt); pos = pos + sizelat + 4;
  lng << it->getlong(); txt = lng.str();      line.insert(pos,txt); pos = pos + sizelng + 4;
  error << it->geterror(); txt = error.str(); line.insert(pos,txt); pos = pos + sizeerror + 4;
  txt = it->getstate().c_str();               line.insert(pos,txt); pos = pos + sizestate + 4;
  depth << it->getdepth(); txt = depth.str(); line.insert(pos,txt); pos = pos + sizedepth + 4;
  mag << it->getmag(); txt = mag.str();       line.insert(pos,txt); pos = pos + sizemag + 4;
  type << it->gettype(); txt = type.str();    line.insert(pos,txt); pos = pos + sizetype + 4;
  txt = it->getcat();                         line.insert(pos,txt); pos = pos + sizecat + 4;
  txt = it->getIo().c_str();                  line.insert(pos,txt); pos = pos + sizeIo + 4;
  area << it->getarea(); txt = area.str();    line.insert(pos,txt); pos = pos + sizearea + 4;

  if (check)
  {
   Mw1 << it->getMw1(); txt = Mw1.str();
  }
  else
   txt = ' ';
  line.insert(pos,txt); 
  pos = pos + sizeMw1 + 4;

  if (check)
  {
   Mw2 << it->getMw2(); txt = Mw2.str();
  }
  else
   txt = ' ';
  line.insert(pos,txt); 
  pos = pos + sizeMw2 + 4;

  if (check)
  {
   Mw3 << it->getMw3(); txt = Mw3.str();
  }
  else
   txt = ' ';
  line.insert(pos,txt); 
  pos = pos + sizeMw3 + 4;

  if (check)
  {
   Mw << it->getMw(); txt = Mw.str();
  }
  else
   txt = ' ';
  line.insert(pos,txt); 
  pos = pos + sizeMw + 4;

  txt = it->getlocal().c_str();               line.insert(pos,txt); pos = pos + sizelocal + 4;
  txt = it->getcomment().c_str();   
  if (txt.size() == 0)
	  txt = ' ';
  line.insert(pos,txt); pos = pos + sizecomment + 4;

  myfile << line << "\n";
  line.clear();
  pos = 0;

 }

 
 myfile.close();
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void FileSystem::readFile( string _fname )
{
 //FILE* pFile = fopen( _fname.c_str(), "r");
 testecat = false;
 char buff[500];
 string extDAT( ".dat" ); // Strings de teste
 string extDAT2( ".csv" );
 const char *newfname = _fname.c_str();

 ifstream fileIn( newfname );
 ifstream fileIn2 (newfname);
 string line;

 int year; string date; string hour;
 double lat; double longg; int error;
 string state; double depth; double mag;
 int type; char cat; string io; double area;
 string local; string comment; string teste1;
 string teste2;
 
 int number_of_lines = 0;
 while (std::getline(fileIn, line))
 {
  if (line.size() != 0)
  {
   ++number_of_lines;
   teste2.append(line,0,4);
   if (teste2 == "YEAR" || _fname.rfind(extDAT2) != string::npos)
   {
	   testecat = true;
   }
  }	 
 }

 fileIn.close();

	for (int i=0; i<number_of_lines; i++)
	{
	 fileIn2.getline(buff,500);
	 string line2(buff);
	 stringstream ss(line2);

  string str2;
  size_t found = line2.find("File generated by EqC");  
  if ( found!=std::string::npos)
  {   
   if (testecat)
   {
    readeqcfile3( newfname, number_of_lines);
   }
   else
   {
    readeqcfile( newfname, number_of_lines);
   }
   return;
  }

  if ( testecat )
  {
	  readeqcfile2( newfname, number_of_lines);
	  return;
  }

	 ss>>year; ss>>date; ss>>hour; ss>>lat;
	 ss>>longg; ss>>error; ss>>state; ss>>depth;
	 ss>>mag; ss>>type; ss>>cat; ss>>io; ss>>teste1;

	 if (teste1[0] >= 48 && teste1[0] <= 57 || teste1[0] == 46)
	 {
		 area = atof(teste1.c_str()); 
   ss>>local;
   comment.clear();
   getlocal(local, comment, line2);
	 }
	 else
	 {
		 area = 0; 
   comment.clear();
   getlocal(teste1, comment, line2);
   local = teste1;
	 }

	 Data fdata(year,date,hour,lat,longg,error,state,depth,mag,type,cat,io,area,local,comment);

	 CONTROLLER.pushData(fdata);
	}

  
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void FileSystem::getlocal( string &loc, string &com, string line )
{
 string str222;
 str222.append(line,60,line.size()-60);
 line = str222;
 str222.clear(); 

 size_t found = line.find(loc);
 int teste = 0; int j = 0;
 char buff[100];

 for (int i=found; i<line.size(); i++)
 {
   if (line[i] == 32)
   {
    if (line[i+1] != 32 && (i+1) < line.size() && line[i+1] != '(')
    {
     loc.push_back(32);
     while( line[i+1+j] != 32 )
     {
       loc.push_back(line[i+1+j]); 
       j++;

       if ((i+1+j) == line.size()-1 || (i+1+j) >= 87)
       {
         loc.push_back(line[i+1+j]);         
         int tf = line.size()-1;  int ti = loc.size();
         for (int k=found+ti; k<=tf; k++)
         {
          com += line[k];
         }
         if (com[0] == 32)
         {
          com.erase(0,1);
         }
         return;
       } 

     }
     j=0;
     teste--;
    }
    teste++;
	if (line[i+1] == 32)
	{
	  teste++;
	}
   }

   if (teste == 2)
   {
     int tf = line.size()-1;
     while (line[i] == 32 && i < tf-1)
     {
      i++;
     }
     for (int j=i; j<=tf; j++)
     {
      com += line[j];
     }         
     return;
   }

 }

 com += " ";
 return;
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void FileSystem::readeqcfile(const char *newfname, int num )
{
	int year; string date; string hour;
	double lat; double longg; int error;
	string state; double depth; double mag;
	int type; char cat; string io; double area;
	string local; string comment; double mag2;

	ifstream fileIn2 (newfname);

	char buff[500];
	fileIn2.getline(buff,500);
	fileIn2.getline(buff,500);

	for (int i=0; i<=num; i++)
	{
		fileIn2.getline(buff,500);
  int j=0;
  string line;

  do //year
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  stringstream ss(line);
  ss>>year;
  j = position(j, buff);
  line.clear();

  do //date
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  date = line;
  j = position(j, buff);
  line.clear();

  do //hour
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  hour = line;
  j = position(j, buff);
  line.clear();

  do //latitude
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  lat = ::atof(line.c_str());
  j = position(j, buff);
  line.clear();

  do //longitude
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  longg = ::atof(line.c_str());
  j = position(j, buff);
  line.clear();

  do //error
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  error = ::atoi(line.c_str());
  j = position(j, buff);
  line.clear();

  do //state
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  state = line;
  j = position(j, buff);
  line.clear();

  do //depth
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  depth = ::atof(line.c_str());
  j = position(j, buff);
  line.clear();

  do //magnitude
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  mag = ::atof(line.c_str());
  j = position(j, buff);
  line.clear();

  do //type
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  type = ::atoi(line.c_str());
  j = position(j, buff);
  line.clear();

  do //cat
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  cat = line[0];
  j = position(j, buff);
  line.clear();

  do //Io
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  io = line;
  j = position(j, buff);
  line.clear();

  do //area
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  area = ::atof(line.c_str());
  j = position(j, buff);
  line.clear();

  do //local
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  local = line;
  j = position(j, buff);
  line.clear();

  do //comment
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  comment = line;
  j = position(j, buff);
  line.clear();

  do //mag2
  {
   line += buff[j];
   j++;
  } while (buff[j] != 0);
  mag2 = ::atof(line.c_str());
  line.clear();

  Data fdata(year,date,hour,lat,longg,error,state,depth,mag,type,cat,io,area,local,comment,-10,-10,-10, -10);
  CONTROLLER.pushData(fdata);

	}

 return;
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void FileSystem::readeqcfile2( const char *newfname, int _num )
{

	int year; string date; //string month; string day; string hour; string minute;
	string hour; double lat; double longg; int error;
	string state; double depth; double mag; int mt;
	char cat; string io; double afelt; //string second;
	string local; string comment; //double mag2;

	ifstream fileIn2 (newfname);

	char buff[500];
 
	for (int i=0; i<_num; i++)
	{
		fileIn2.getline(buff,500);

		int j=0;
		string line;

		do //year
		{
			line += buff[j];
			j++;			
		} while (buff[j] != 44);
		stringstream ss(line);
		ss>>year;
		j++;
		line.clear();


		do //month
		{
			if (buff[j] == 32)
			{line += "0";} 
			else
			{line += buff[j];}							
			j++;
		} while (buff[j] != 44);
		//month = line;
		j++;
		//line.clear();
		line += "/";

		do //day
		{
			if (buff[j] == 32)
			{line += "0";} 
			else
			{line += buff[j];}							
			j++;
		} while (buff[j] != 44);
		//day = line;
		date = line;
		j++;
		line.clear();

		j++;
		do //hour
		{
			if (buff[j] == 32)
			{line += "0";} 
			else
			{line += buff[j];}							
			j++;
		} while (buff[j] != 44);
		//hour = line;
		j++;
		//line.clear();
		line += ":";

		do //minute
		{
			if (buff[j] == 32)
			{line += "0";} 
			else
			{line += buff[j];}							
			j++;
		} while (buff[j] != 44);
		//minute = line;
		j++;
		//line.clear();
		line += ":";

		j++;j++;j++;
		do //second
		{
			if (buff[j] == 32)
			{line += "0";} 
			else
			{line += buff[j];}							
			j++;
		} while (buff[j] != 44);
		//second = line;
		hour = line;
		j++;
		line.clear();

		do //latitude
		{
			line += buff[j];
			j++;
		} while (buff[j] != 44);
		lat = ::atof(line.c_str());
		j++;
		line.clear();

		do //longitude
		{
			line += buff[j];
			j++;
		} while (buff[j] != 44);
		longg = ::atof(line.c_str());
		j++;
		line.clear();

		do //depth
		{
			line += buff[j];
			j++;
		} while (buff[j] != 44);
		depth = ::atof(line.c_str());
		j++;
		line.clear();

		do //error
		{
			line += buff[j];
			j++;
		} while (buff[j] != 44);
		error = ::atoi(line.c_str());
		j++;
		line.clear();

		do //magnitude
		{
			line += buff[j];
			j++;
		} while (buff[j] != 44);
		mag = ::atof(line.c_str());
		j++;
		line.clear();

		do //mt
		{
			line += buff[j];
			j++;
		} while (buff[j] != 44);
		mt = ::atoi(line.c_str());
		j++;
		line.clear();

		do //cat
		{
			line += buff[j];
			j++;
		} while (buff[j] != 44);
		cat = line[0];
		j++;
		line.clear();

		do //Io
		{
			line += buff[j];
			j++;
		} while (buff[j] != 44);
		io = line;
		j++;
		line.clear();

		do //afelt
		{
			line += buff[j];
			j++;
		} while (buff[j] != 44);
		afelt = ::atof(line.c_str());
		j++;
		line.clear();
		
		do //state
		{
			line += buff[j];
			j++;
			
		} while (buff[j] != 44);
		state = line;
		j++;
		line.clear();

		int k=0;		
		do //local
		{
			if (k <= 2)
			line += buff[j];

			if (buff[j] == 32)
			k++;
			else
			k = 0;
			
			j++;
		} while (buff[j] != 44);
		local = line;
		j++;
		line.clear();

		do //comment
		{
			line += buff[j];
			j++;
		} while (buff[j] != 44);
		comment = line;
		j++;
		line.clear();

		Data fdata(year,date,hour,lat,longg,error,state,depth,mag,mt,cat,io,afelt,local,comment);		 
		CONTROLLER.pushData(fdata);
	}

}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
int FileSystem::position( int a, const char ( &s )[500] )
{
	while (s[a] == 0)
	{
		a++;
	}

	return a;
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void FileSystem::writeFile2( string _fname )
{
	// Write CSV
	const char *newfname = _fname.c_str();
	ofstream myfile(newfname);
	string txt;
	string line;
	string file_title;

    // CHARACTER ',' OR ';'
	string character = CONTROLLER.getCharacter();
    
	// print title and time 
	file_title += "File generated by EqC  -  ";
	time_t rawtime;
	time (&rawtime);
	file_title += ctime(&rawtime);
	myfile << file_title << "\n";

	vector<Data> newDataSheet = CONTROLLER.getDataSheet();
	std::vector<Data>::iterator it;

	bool check = CONTROLLER.calcMagSetted();
	
	int maxSize = 500;

	for(it = newDataSheet.begin(); it != newDataSheet.end(); ++it)
	{
		stringstream year;  stringstream lat;   stringstream lng;
		stringstream error; stringstream depth; stringstream mag; 
		stringstream type;  stringstream area;  stringstream mag2;
		stringstream mw1;   stringstream mw2;   stringstream mw3;
  stringstream mw;
		
		year << it->getyear(); txt = year.str();    line.append(txt);  line.append(character);  //YEAR
		txt = it->getdate().c_str();                line.append(txt);  line.append(character);  //DATE 
		txt = it->gethour().c_str();                line.append(txt);  line.append(character);  //HOUR
		lat << it->getlat(); txt = lat.str();       line.append(txt);  line.append(character);  //LATITUDE
		lng << it->getlong(); txt = lng.str();      line.append(txt);  line.append(character);  //LONGITUDE
		error << it->geterror(); txt = error.str(); line.append(txt);  line.append(character);
		txt = it->getstate().c_str();               line.append(txt);  line.append(character);
		depth << it->getdepth(); txt = depth.str(); line.append(txt);  line.append(character);
		mag << it->getmag(); txt = mag.str();       line.append(txt);  line.append(character);
		type << it->gettype(); txt = type.str();    line.append(txt);  line.append(character);
		txt = it->getcat();                         line.append(txt);  line.append(character);
		txt = it->getIo().c_str();                  line.append(txt);  line.append(character);
		area << it->getarea(); txt = area.str();    line.append(txt);  line.append(character);
		mw1 << it->getMw1(); txt = mw1.str();       line.append(txt);  line.append(character);  //MW1
  mw2 << it->getMw2(); txt = mw2.str();       line.append(txt);  line.append(character);  //MW2
  mw3 << it->getMw3(); txt = mw3.str();       line.append(txt);  line.append(character);  //MW3
  mw  << it->getMw();  txt = mw.str();        line.append(txt);  line.append(character);  //MW
		txt = it->getlocal().c_str();               line.append(txt);  line.append(character);  //LOCAL
		txt = it->getcomment().c_str();                                                         //COMMENT

		if (txt.size() == 0)
			txt = " ";

		line.append(txt);   line.append(character);   txt = ' ';

		line.append(txt);
		myfile << line << "\n";
		line.clear();		

	}


	myfile.close();
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
void FileSystem::readeqcfile3( const char *newfname, int _num )
{

 int year; string date; //string month; string day; string hour; string minute;
 string hour; double lat; double longg; int error;
 string state; double depth; double mag; int mt;
 char cat; string io; double afelt; //string second;
 string local; string comment; //double mag2;

 ifstream fileIn2 (newfname);
 char buff[500];

 fileIn2.getline(buff,500);
 fileIn2.getline(buff,500);

 for (int i=0; i<_num-1; i++)
 {  
  fileIn2.getline(buff,500);

  int j=0;
  string line;

  do //year
  {
   line += buff[j];
   j++;			
  } while (buff[j] != 44 && buff[j] != 59);
  stringstream ss(line);
  ss>>year;
  j++;
  line.clear();


  do //month
  {   
   line += buff[j];						
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  date = line;
  j++;
  line.clear();

  do //hour
  {   
   line += buff[j];						
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  hour = line;
  j++;
  line.clear();  

  do //latitude
  {
   line += buff[j];
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  lat = ::atof(line.c_str());
  j++;
  line.clear();

  do //longitude
  {
   line += buff[j];
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  longg = ::atof(line.c_str());
  j++;
  line.clear();
  
  do //error
  {
   line += buff[j];
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  error = ::atoi(line.c_str());
  j++;
  line.clear();

  do //state
  {
   line += buff[j];
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  state = line;
  j++;
  line.clear();

  do //depth
  {
   line += buff[j];
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  depth = ::atof(line.c_str());
  j++;
  line.clear();

  do //magnitude
  {
   line += buff[j];
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  mag = ::atof(line.c_str());
  j++;
  line.clear();

  do //mt
  {
   line += buff[j];
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  mt = ::atoi(line.c_str());
  j++;
  line.clear();

  do //cat
  {
   line += buff[j];
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  cat = line[0];
  j++;
  line.clear();

  do //Io
  {
   line += buff[j];
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  io = line;
  j++;
  line.clear();

  do //afelt
  {
   line += buff[j];
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  afelt = ::atof(line.c_str());
  j++;
  line.clear();

  do //magnitude mw1(m)
  {
   line += buff[j];
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  mag = ::atof(line.c_str());
  j++;
  line.clear();

  do //magnitude mw2(a)
  {
   line += buff[j];
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  mag = ::atof(line.c_str());
  j++;
  line.clear();

  do //magnitude mw3(m,a)
  {
   line += buff[j];
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  mag = ::atof(line.c_str());
  j++;
  line.clear();

  do //magnitude mw
  {
   line += buff[j];
   j++;
  } while (buff[j] != 44 && buff[j] != 59);
  mag = ::atof(line.c_str());
  j++;
  line.clear();

  do //local
  {
    line += buff[j];
    j++;
  } while (buff[j] != 44 && buff[j] != 59);
  local = line;
  j++;
  line.clear();

  //comment
  line.append(buff,j,150);
  line.erase(line.size()-2,2);
  comment = line;
  line.clear();

  Data fdata(year,date,hour,lat,longg,error,state,depth,mag,mt,cat,io,afelt,local,comment);		 
  CONTROLLER.pushData(fdata);
 }

}